<template>
  <el-dialog
    :title="dialogType ==='dialog_add'? text_add : text_update"
    :close-on-click-modal="isDebug"
    :visible.sync="dialogVisible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="130px" label-position="right">
      <el-form-item label="老师id" prop="teacherId">
        <el-input v-model="dataForm.teacherId" placeholder="老师id"></el-input>
      </el-form-item>
      <el-form-item label="直播室id" prop="liveId">
        <el-input v-model="dataForm.liveId" placeholder="直播室id"></el-input>
      </el-form-item>
      <el-form-item label="开课时间" prop="startTime">
        <el-input v-model="dataForm.startTime" placeholder="开课时间"></el-input>
      </el-form-item>
      <el-form-item label="开课时间" prop="endTime">
        <el-input v-model="dataForm.endTime" placeholder="开课时间"></el-input>
      </el-form-item>
      <el-form-item label="周一到周日(1-7表示)" prop="week">
        <el-input v-model="dataForm.week" placeholder="周一到周日(1-7表示)"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          value-format="timestamp"
          placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">{{text_cancel}}</el-button>
      <el-button type="primary"
                 @click="dialogType ==='dialog_add'?requestAdd():requestUpdate()">{{text_confirm}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dialogVisible: false,
        dialogType: 'dialog_add', // 弹窗类型 1添加 2编辑,
        dataForm: {
          id: 0,
          teacherId: '',
          liveId: '',
          startTime: '',
          endTime: '',
          week: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          teacherId: [{validator: this.checkText, trigger: 'change'}],
          liveId: [{validator: this.checkText, trigger: 'change'}],
          startTime: [{validator: this.checkText, trigger: 'change'}],
          endTime: [{validator: this.checkText, trigger: 'change'}],
          week: [{validator: this.checkText, trigger: 'change'}],
          createTime: [{validator: this.checkText, trigger: 'change'}],
          updateTime: [{validator: this.checkText, trigger: 'change'}]
        }
      }
    },
    props: {
      rowData: {
        type: Object,
        default: null
      }
    },
    methods: {
      initDialog (dialogType) {
        this.dialogVisible = true
        this.dialogType = dialogType
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (dialogType === 'dialog_update') {
            this.requestInfo()
          }
        })
      },
      /** 请求信息(表单回显) */
      requestInfo () {
        this.$httpRequest({
          url: `/api/course/info/${this.rowData.id}`,
          method: 'get'
        }).then((data) => {
          this.dataForm.teacherId = data.teacherId
          this.dataForm.liveId = data.liveId
          this.dataForm.startTime = data.startTime
          this.dataForm.endTime = data.endTime
          this.dataForm.week = data.week
          this.dataForm.createTime = data.createTime
          this.dataForm.updateTime = data.updateTime
        }, error => {
          this.showErrorToast(error.msg)
        })
      },
      /** 请求添加 */
      requestAdd () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/course/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'teacherId': this.dataForm.teacherId,
                'liveId': this.dataForm.liveId,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'week': this.dataForm.week,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      },
      /** 请求更新 */
      requestUpdate () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/course/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'teacherId': this.dataForm.teacherId,
                'liveId': this.dataForm.liveId,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'week': this.dataForm.week,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      }
    }
  }
</script>
